package gatilhos;

import org.anddev.andengine.audio.sound.SoundManager;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.TextureManager;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.content.Context;

public class Detonador extends Gatilho {

	private BitmapTextureAtlas mapaDeTexturas;
	private TiledTextureRegion textura;

	public Detonador(int x, int y,Context contexto, TextureManager tm, SoundManager sm){
		this.x = x;
		this.y = y;
		largura = 64;
		altura = 64;
		
		mapaDeTexturas = new BitmapTextureAtlas(128, 64, TextureOptions.BILINEAR);
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/gatilhos/");
		textura = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mapaDeTexturas, contexto, "Gatilho #4.png", 0, 0, 2, 1);
		tm.loadTexture(mapaDeTexturas);
		
		spriteAnimado = new AnimatedSprite(x, y, textura);
	}

	@Override
	public void atualiza() {
		if(ativado && spriteAnimado.getCurrentTileIndex() == 0){
			spriteAnimado.setCurrentTileIndex(1);
		}
		if(!ativado && spriteAnimado.getCurrentTileIndex() == 1){
			spriteAnimado.setCurrentTileIndex(0);
		}
	}
	
	@Override
	public void atualizaSprite() {

	}
	
	@Override
	public void tocaSom() {
	}

	@Override
	public boolean colidiuComCaoGuia(Rectangle spriteCaoGuia) {
		if(spriteCaoGuia.collidesWith(spriteAnimado)){
			return true;
		}else{
			return false;
		}
	}
	
	@Override
	public void trataColisaoComCaoGuia(Rectangle spriteCaoGuia) {
		ativado = true;
	}
	
	@Override
	public void trataNaoColisaoComCaoGuia() {
		ativado = false;
	}

	@Override
	public void posicionaNaCena(int mapx, int mapy) {
		spriteAnimado.setPosition((int)x-mapx, (int)y-mapy);
	}

}
